package com.unswift.window.factory;

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import com.unswift.utils.ObjectUtils;
import com.unswift.window.extend.Attribute;
import com.unswift.window.table.Table;
import com.unswift.window.table.TableModel;
import com.unswift.window.table.TableTitle;

public class TableFactory {

	public static Table create(TableTitle[] titles, Object[][] data, String action){
		TableModel model=new TableModel(titles, data);
		Table table=new Table(model, action);
		table.setRowHeight(22);
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		table.setRowSelectionAllowed(true);
		table.setColumnSelectionAllowed(false);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setSelectionBackground(new Color(0,120,215));
		table.setSelectionForeground(Color.WHITE);
		table.setGridColor(new Color(220, 220, 220));
		table.getTableHeader().setBackground(Color.WHITE);
		table.getTableHeader().setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(187, 187, 187)));
		for (int i = 0, length=titles.length; i < length; i++) {
			columnEdit(
				Attribute.create()
				.put(Attribute.ATTRIBUTE_TABLE, table)
				.put(Attribute.ATTRIBUTE_TABLE_COLUMN_INDEX, i)
				.put(Attribute.ATTRIBUTE_TABLE_COLUMN_MIN_WIDTH, titles[i].getMinWidth())
				.put(Attribute.ATTRIBUTE_TABLE_COLUMN_MAX_WIDTH, titles[i].getMaxWidth())
			);
		}
		return table;
	}
	
	public static void columnEdit(Attribute attribute){
		Table table=attribute.get(Attribute.ATTRIBUTE_TABLE);
		int columnIndex=attribute.get(Attribute.ATTRIBUTE_TABLE_COLUMN_INDEX);
		TableColumn column = table.getColumnModel().getColumn(columnIndex);
		Integer minWidth = attribute.get(Attribute.ATTRIBUTE_TABLE_COLUMN_MIN_WIDTH);
		if(ObjectUtils.isNotEmpty(minWidth)){
			column.setMinWidth(minWidth);
		}
		Integer maxWidth = attribute.get(Attribute.ATTRIBUTE_TABLE_COLUMN_MAX_WIDTH);
		if(ObjectUtils.isNotEmpty(maxWidth)){
			column.setMaxWidth(maxWidth);
		}
	}
	
	public static void addRowData(Table table, Object[] rowData){
		((DefaultTableModel)table.getModel()).addRow(rowData);
		int rowCount=table.getRowCount()-1;
		table.setRowSelectionInterval(rowCount, rowCount);
		table.setColumnSelectionInterval(1, 1);
	}
	
	public static void deleteAll(JTable table){
		if(table.getRowCount()>0 && table.getColumnCount()>0){
			setSelectRow(table, 0, 0);
			removeSelectRow(table, 0, 0);
		}
		DefaultTableModel model = (DefaultTableModel)table.getModel();
		int row=table.getRowCount();
		for (int i = row-1; i >= 0; i--) {
			model.removeRow(i);
		}
	}
	
	public static void setSelectRow(JTable table, int row, Integer column){
		table.setRowSelectionInterval(row, row);
		if(ObjectUtils.isNotEmpty(column)){
			table.setColumnSelectionInterval(column, column);
		}
	}
	
	public static void removeSelectRow(JTable table, int row, Integer column){
		table.removeRowSelectionInterval(row, row);
		if(ObjectUtils.isNotEmpty(column)){
			table.removeColumnSelectionInterval(column, column);
		}
	}
}
